package gamecomponents.levelcomponents;
import gamecomponents.character.Player;
import gamecomponents.levelcomponents.stagelements.Block;
import gamecomponents.levelcomponents.stagelements.buildingmats.Door;
import gamecomponents.levelcomponents.stagelements.playercontrol.Physics;
import gamecomponents.navigation.Direction;
import gamecomponents.navigation.Location;
import gamecomponents.skeletontesthelper.Assistant;
import gamecomponents.skeletontesthelper.Interaction;
import gamecomponents.skeletontesthelper.Parameter;
import java.io.BufferedReader;
import java.util.ArrayList;

/**
 * @author Akos
 * @version 1.0
 * @created 14-márc.-2012 16:36:09
 */
public class Stage {

	/**
	 * Referencia lista az összes blokkról
	 */
	private ArrayList<Block> blocks;
	/**
	 * Ide támad fel a hulla.
	 */
	
	private Door door;
	private PerspectiveState perState;
	private Physics ph;
	private Location startPoint;
	public PerspectiveState m_PerspectiveState;

        public Stage(BufferedReader bRead, Physics phi){
            Parameter param[]=new Parameter[2];
            param[0]=new Parameter(bRead);
            param[1]=new Parameter(phi);
            Assistant.write(Interaction.CRT,this,"Stage",param);
            perState = PerspectiveState.BLOCKCHANGING_MODE;
                    //Kezdetben Blokkváltás nézet
            //TODO Stage kostruktor!!!
            ph=phi;     //Phisics elmentése
            
	}

	public void finalize() throws Throwable {

	}

	public void addNewMovement(Direction dir){      //Fizikának szólunk, 
            Parameter param[]=new Parameter[1];         //új mozgatás parancs
            param[0]=new Parameter(dir);
            Assistant.write(Interaction.CALL,this,"addNewMovement",param);
            
            ph.addForceDirection(dir);
            
            param[0]=new Parameter(null);
            Assistant.write(Interaction.RET,this,"addNewMovement",param);
	}

	/**
	 * perspektívaváltás
	 */
	public void changePerspective(){   
            Parameter param[]=new Parameter[1];
            param[0]=new Parameter(null);
            Assistant.write(Interaction.CALL,this,"changePerspective",param);
            
            if(perState==PerspectiveState.BLOCKCHANGING_MODE){
                perState=PerspectiveState.PLAYERMOVING_MODE;
                ph.startTimer();    //A Timer elindul, elindul a fizika
            }
            else{
                perState=PerspectiveState.BLOCKCHANGING_MODE;
                ph.stopTimer();     //A Timer leáll, leáll a fizika
            }
            
            Assistant.write(Interaction.RET,this,"changePerspective",param);
	}

	public Location getStartPointsLocation(){   //A kezdőpozíció helyének visszaadása
            Parameter param[]=new Parameter[1];
            param[0]=new Parameter(null);
            Assistant.write(Interaction.CALL,this,"getStartPointsLocation",param);
                     
            param[0]=new Parameter(startPoint);
            Assistant.write(Interaction.RET,this,"getStartPointsLocation",param);
            
            return startPoint;
	}

	/**
	 * Mozgás
	 * 
	 */
	public void move(Direction dir){    //Új irány érkezett a felhasználótól
            Parameter param[]=new Parameter[1]; //Azaz, egy nyíl billentyűnyomás
            param[0]=new Parameter(dir);
            Assistant.write(Interaction.CALL,this,"move",param);
            
            if(perState==PerspectiveState.BLOCKCHANGING_MODE)
                this.moveBlock(dir);
            else
                this.addNewMovement(dir);
                
            param[0]=new Parameter(null);
            Assistant.write(Interaction.RET,this,"move",param);
	}

	/**
	 * 
	 * @param dir
	 */
	private void moveBlock(Direction dir){
            Parameter param[]=new Parameter[1];
            param[0]=new Parameter(dir);
            Assistant.write(Interaction.CALL,this,"moveBlock",param);
            
            Block empty = this.getEmptyBlock();
            Direction oppDir=Direction.getOpposite(dir);
            empty.switchPlaces(oppDir);
            
            param[0]=new Parameter(null);
            Assistant.write(Interaction.RET,this,"moveBlock",param);
	}

	/**
	 * Ha meghal a figura, ezzel jelez,ő fogja újraéleszteni majd a figurát
	 */
	public void playerDead(Player player){
            Parameter param[]=new Parameter[1];
            param[0]=new Parameter(null);
            Assistant.write(Interaction.CALL,this,"playerDead",param);
                     
            //TODO Inkonzisztencia!! Ezért még nincs kész!!!
            
            param[0]=new Parameter(null);
            Assistant.write(Interaction.RET,this,"playerDead",param);
	}

	private Block getEmptyBlock(){
            Parameter param[]=new Parameter[1];
            param[0]=new Parameter(null);
            Assistant.write(Interaction.CALL,this,"getEmptyBlock",param);
            
            //TODO getEmptyBlock nincs implementálva!!
            Assistant.setName("emptyBlock");
            Block empty = new Block();
            
            param[0]=new Parameter(empty);
            Assistant.write(Interaction.RET,this,"getEmptyBlock",param);
            
            return empty;
	}

}